//
//  HWWebViewGameViewController.m
//  94hwanGame
//
//  Created by 王治庭 on 2019/3/21.
//  Copyright © 2019 王治庭. All rights reserved.
//

#import "HWWebViewGameViewController.h"
#import <WebKit/WebKit.h>
#import "HWHeaderView.h"
#import "HWGameWebView.h"

#import <WebKit/WebKit.h>

@interface HWWebViewGameViewController ()<WKUIDelegate,WKNavigationDelegate>{
    
    NSString *member_id; //推广id
    
}

@property (strong, nonatomic) HWHeaderView *header;
@property (strong,nonatomic) HWGameWebView *webView1 ;

@end

@implementation HWWebViewGameViewController

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([self.isLandscape isEqualToString:@"1"]) {
        return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //    self.navigationItem.title = self.gname;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    
    HWGameWebView *webView = [HWGameWebView initWithUrlString:self.urlString];
    self.webView1 =webView;
    self.webView1.kongzhiView = self;
    [self.view addSubview:webView];
    
    self.header = [HWHeaderView new];
    self.header.backgroundColor = UIColor.whiteColor;
    self.header.title.textColor = UIColor.blackColor;
    self.header.title.text = self.gname;
    __weak typeof(self) weakSelf = self;
    self.header.leftClick = ^() {
        [[HWViewUtils getCurrentVC].navigationController popViewControllerAnimated:YES];
        
        if (self.isPush == YES) {
            weakSelf.tabBarController.tabBar.hidden = YES;
            
        } else {
            weakSelf.tabBarController.tabBar.hidden = NO;
        }
        
    };
    [self.view addSubview:self.header];
    
    [self.header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(self.view.mas_left);
        make.height.mas_equalTo(44 + StatusBarHeight);
        make.right.mas_equalTo(self.view.mas_right);
    }];
    
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.header.mas_bottom);
        make.left.mas_equalTo(self.view.mas_left);
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.right.mas_equalTo(self.view.mas_right);
    }];
    
    
    if ([self.panduanStr isEqualToString:@"1"]) {
        
        //        UIButton *jiluBut = [UIButton buttonWithType:UIButtonTypeCustom];
        //        jiluBut.frame = CGRectMake(0, 0, 48, 25);
        //        jiluBut.backgroundColor = RGB(255, 255, 255);
        //        jiluBut.layer.cornerRadius = 2;
        //        [jiluBut setTitle:@"记录" forState:UIControlStateNormal];
        //        [jiluBut addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
        //        jiluBut.layer.masksToBounds = YES;
        UIImage *loginImg = [UIImage imageNamed:@"fanhui"];
        loginImg = [loginImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:loginImg style:UIBarButtonItemStylePlain target:self action:@selector(fanhui)];
        
        UIButton *jiluBut = [UIButton buttonWithType:UIButtonTypeCustom];
        jiluBut.frame = CGRectMake(0, 0, 48, 25);
        jiluBut.backgroundColor = RGB(247, 124, 47);
        jiluBut.layer.cornerRadius = 2;
        [jiluBut setTitle:@"记录" forState:UIControlStateNormal];
        [jiluBut addTarget:self action:@selector(jiluButTap) forControlEvents:UIControlEventTouchUpInside];
        jiluBut.layer.masksToBounds = YES;
        //        self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]initWithCustomView:jiluBut];
        [self.header addSubview:jiluBut];
        [jiluBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-14);
            make.width.mas_equalTo(48);
            make.height.mas_equalTo(25);
            make.centerY.mas_equalTo(self.header.leftButton);
        }];
        
        
        
        
    } else if ([self.panduanStr isEqualToString:@"2"]) {
        
        UIImage *loginImg = [UIImage imageNamed:@"fanhui"];
        loginImg = [loginImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:loginImg style:UIBarButtonItemStylePlain target:self action:@selector(fanhui)];
        
        UIButton *jiluBut = [UIButton buttonWithType:UIButtonTypeCustom];
        jiluBut.frame = CGRectMake(0, 0, 48, 25);
        jiluBut.backgroundColor = RGB(247, 124, 47);
        jiluBut.layer.cornerRadius = 2;
        [jiluBut setTitle:@"记录" forState:UIControlStateNormal];
        [jiluBut addTarget:self action:@selector(jiluButTap) forControlEvents:UIControlEventTouchUpInside];
        jiluBut.layer.masksToBounds = YES;
        //        self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]initWithCustomView:jiluBut];
        [self.header addSubview:jiluBut];
        [jiluBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-14);
            make.width.mas_equalTo(48);
            make.height.mas_equalTo(25);
            make.centerY.mas_equalTo(self.header.leftButton);
        }];
        
        
    }
    
    
}

- (void)initFrame {
    UIInterfaceOrientation interfaceOritation = [[UIApplication sharedApplication] statusBarOrientation];
    
    [self changeInterface:interfaceOritation];
}

- (void)orientationChange:(NSNotification *)notification {
    
    UIInterfaceOrientation interfaceOritation = [[UIApplication sharedApplication] statusBarOrientation];
    [self changeInterface:interfaceOritation];
}
- (void)changeInterface:(UIInterfaceOrientation)oritation {
    
    if (oritation == UIInterfaceOrientationPortrait) {
        [self.header mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view.mas_top);
            make.left.mas_equalTo(self.view.mas_left);
            make.height.mas_equalTo(44 + StatusBarHeight);
            make.right.mas_equalTo(self.view.mas_right);
        }];
        
        [self.webView1 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.header.mas_bottom);
            make.left.mas_equalTo(self.view.mas_left);
            make.bottom.mas_equalTo(self.view.mas_bottom);
            make.right.mas_equalTo(self.view.mas_right);
        }];
    }else if (oritation == UIInterfaceOrientationLandscapeRight|oritation == UIInterfaceOrientationLandscapeLeft) {
        
        [self.header mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view.mas_top);
            make.left.mas_equalTo(self.view.mas_left);
            make.height.mas_equalTo(44);
            make.right.mas_equalTo(self.view.mas_right);
        }];
        
        [self.webView1 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.header.mas_bottom);
            make.left.mas_equalTo(self.view.mas_left);
            make.bottom.mas_equalTo(self.view.mas_bottom);
            make.right.mas_equalTo(self.view.mas_right);
        }];
    }
}

- (void)fanhui {
    //   self.webView ->_webView
    if([self.webView1 ->_webView canGoBack]){
        [self.webView1 ->_webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
    
}



- (void)jiluButTap {
    
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    
    NSString *path2 = [NSString stringWithFormat:@"%@/%@", bundlePath, @"coid"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    //        NSString *channel = @"";
    
    
    NSString *channel = [NSString string];
    if ([manager fileExistsAtPath:path2]) {
        NSArray *allPath =[manager subpathsAtPath:path2];
        //4.遍历所有的子路径
        for (NSString *subPath in allPath) {
            if ([subPath containsString:@"coid_"]) {
                channel = [subPath substringFromIndex:@"coid_".length];
                NSLog(@"文件夹名字%@+++",  channel);
            }
        }
    }
    if ([channel isEqual:@""]) {
        member_id = [NSString stringWithFormat:@"0"];
    } else {
        member_id = [NSString stringWithFormat:@"%@",channel];
    }
    NSLog(@"有值吗++%@+++", member_id);
    
    
    if ([self.panduanStr isEqualToString:@"1"] ) {
        
        NSString *uid = [HWLocallyStoredData getLoginUserID];
        NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
        //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
        NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
        NSString *signMD5 = [WMUtil md5HexDigest:signStr];
        
        
        NSString *url = [NSString stringWithFormat:@"%@?ct=app2021&ios=1&ac=lb_log&uid=%@&t=%@&sign=%@&gid=%@&member_id=%@",[HWOCGlobalConst baseUrl],[HWLocallyStoredData getLoginUserID],tString,signMD5 ,self.gameId,member_id];
        
        
        
        HWWebViewGameViewController *webview1 = [[HWWebViewGameViewController alloc]init];
        webview1.urlString = url;
        //   webview.gname = @"领取礼包";
        webview1.gname = @"领取记录";
        //   webview.panduanStr = @"1";
        webview1.hidesBottomBarWhenPushed = NO;
        //        webview1.hidesBottomBarWhenPushed = NO;
        webview1.isPush = YES;
        
        [self.navigationController pushViewController:webview1 animated:YES];
        
        
        
    }else if ( [self.panduanStr isEqualToString:@"2"]){
        
        NSString *uid = [HWLocallyStoredData getLoginUserID];
        NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
        //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
        NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
        NSString *signMD5 = [WMUtil md5HexDigest:signStr];
        
        
        NSString *url = [NSString stringWithFormat:@"%@?ct=app2021&ios=1&ac=coupon_log&uid=%@&t=%@&sign=%@&gid=%@&member_id+%@",[HWOCGlobalConst baseUrl],[HWLocallyStoredData getLoginUserID],tString,signMD5 ,self.gameId,member_id];
        
        HWWebViewGameViewController *webview1 = [[HWWebViewGameViewController alloc]init];
        webview1.urlString = url;
        //   webview.gname = @"领取礼包";
        webview1.gname = @"领取记录";
        //   webview.panduanStr = @"1";
        webview1.hidesBottomBarWhenPushed = NO;
        //        webview1.hidesBottomBarWhenPushed = NO;
        webview1.isPush = YES;
        [self.navigationController pushViewController:webview1 animated:YES];
        
        
    }
}


@end
